﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using OnLineExamBLL;

namespace OnlineExam.Web.mag
{
    /// <summary>
    /// GetSingleListHandler 的摘要说明
    /// </summary>
    public class GetSingleListHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            //context.Response.ContentType = "text/plain";
            string type = context.Request.Params["type"];
            SingleSelectedManager bll = new SingleSelectedManager();

            switch (type)
            {
                case "getlist":
                    #region 获取列表
                    int page = int.Parse(context.Request.Params["page"]);
            int rows = int.Parse(context.Request.Params["rows"]);
            string depid = context.Request.Params["depid"];
            int startIndex = (page-1) * rows+1;
            int endIndex = (page) * rows ;

            //var loginuser=new UserManager().GetModel(context.User.Identity.Name);
            var ds = bll.GetListByPage("depid=" + depid, "ID", startIndex, endIndex);
            int RecordCount = bll.GetRecordCount("depid=" + depid);
            context.Response.Write(HHG.Common.Json.DataTable2Json(ds.Tables[0], RecordCount));
                    #endregion
                    break;
                case "edit":
                    #region 根据ID获取信息
                    var ID = context.Request.Params["ID"];
                    if (!string.IsNullOrEmpty(ID))
                    {
                        var m = bll.GetModel(int.Parse(ID));
                        var rsult = HHG.Common.Json.GetJson<OnLineExamModel.SingleProblem>(m);
                        context.Response.Write(rsult);
                    }
                    
                    #endregion
                    break;
                case "addsave":
                    #region 提交修改
                    depid = context.Request.Params["selectDep"];
                    
                    var new_model = new OnLineExamModel.SingleProblem();
                    //new_model.ID = int.Parse(ID);
                    new_model.DepID =int.Parse( depid);
                    new_model.CourseID = int.Parse(context.Request.Params["selectCourse"]);
                    new_model.Title = context.Request.Params["txtTitle"];
                    new_model.AnswerA = context.Request.Params["txtA"];
                    new_model.AnswerB = context.Request.Params["txtB"];
                    new_model.AnswerC = context.Request.Params["txtC"];
                    new_model.AnswerD = context.Request.Params["txtD"];
                    new_model.Answer = context.Request.Params["selectAnswer"];

                    int nID = bll.Add(new_model);
                    context.Response.Write(nID);//修改成功，返回ID.失败，返回0
                     
                    #endregion
                    break;
                case "editsave":
                    #region 提交修改
                    depid = context.Request.Params["selectDep"];
                    ID = context.Request.Params["ID"];
                    var o_model = bll.GetModel(int.Parse(ID));
                    new_model = new OnLineExamModel.SingleProblem();
                    new_model.ID =int.Parse( ID);
                    new_model.DepID=o_model.DepID;
                    new_model.CourseID = int.Parse(context.Request.Params["selectCourse"]);
                    new_model.Title = context.Request.Params["txtTitle"];
                    new_model.AnswerA = context.Request.Params["txtA"];
                    new_model.AnswerB = context.Request.Params["txtB"];
                    new_model.AnswerC = context.Request.Params["txtC"];
                    new_model.AnswerD = context.Request.Params["txtD"];
                    new_model.Answer = context.Request.Params["selectAnswer"];

                    if (bll.Update(new_model))
                    {
                        context.Response.Write(ID);//修改成功，返回ID
                    }
                    else
                    {
                        context.Response.Write("0");//修改失败，返回0
                    }
                    #endregion
                    break;
                case "del":
                    #region 删除
                    ID = context.Request.Params["ID"];
                    if (bll.Delete(int.Parse(ID)))
                    {
                        context.Response.Write(ID);//删除成功，返回ID
                    }
                    else
                    {
                        context.Response.Write("0");//删除失败，返回0
                    }
                    #endregion
                    break;
                default:
                    break;
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}